﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using Entity;

namespace CustomerManager
{
    public partial class ucCustomer : UserControl
    {
        int status;
        public FrmMain frm;
        DataTable dtCaNhan;
        DataTable dtDoanhNghiep;

        public ucCustomer()
        {
            InitializeComponent();
        }
        
        public void LoadCustomer(int s)
        {
            status = s;
            if (status == -1)
            {
                dtCaNhan = PersonalBUS.LayTatCaKHCaNhan();
                dgvCaNhan.DataSource = dtCaNhan;
                dtDoanhNghiep = CompanyBUS.LayTatCaKHDoanhNghiep();
                dgvDoanhNghiep.DataSource = dtDoanhNghiep;

                DataTable dt = NhanVienBUS.LayNhanVienTheoID(status);
                DataRow dr = dt.NewRow();
                dr["Tennv"] = "Tất cả Nhân Viên";
                dr["Manv"] = -1;
                dt.Rows.Add(dr);
                cmbNhanVien.DataSource = dt;
                cmbNhanVien.DisplayMember = "Tennv";
                cmbNhanVien.ValueMember = "Manv";
                btnThemKhachHang.Hide();
                cmbNhanVien.Enabled = true;
            }
            else
            {
                cmbNhanVien.Enabled = false;
                cmbNhanVien.DataSource = NhanVienBUS.LayNhanVienTheoID(status);
                cmbNhanVien.DisplayMember = "Tennv";
                dtCaNhan = PersonalBUS.LayKHCaNhanTheoNhanVien(status);
                dgvCaNhan.DataSource = dtCaNhan;
                dtDoanhNghiep = CompanyBUS.LayKHDoanhNghiepTheoNhanVien(status);
                dgvDoanhNghiep.DataSource = dtDoanhNghiep;
            }

            dgvCaNhan.BringToFront();
            cmbLoaiKhachHang.SelectedIndex = 0;
            cmbTieuChiCaNhan.SelectedIndex = 0;
            cmbNhanVien.SelectedIndex = cmbNhanVien.Items.Count - 1;
            cmbNhanVien_SelectedIndexChanged(new object(), new EventArgs());
            cmbTieuChiCaNhan_SelectedIndexChanged(new object(), new EventArgs());
        }
        public void loadfrmmain()
        {
            frm = (FrmMain)Application.OpenForms["FrmMain"];
        }

        private void btnXemChiTiet_Click(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.Text == "Cá nhân")
            {
                if (dgvCaNhan.CurrentRow == null)
                    return;
                ucPersonal frmPersonal = new ucPersonal(FrmMain.ExeAction.View, this);
                Personal person = new Personal();
                person.Ma = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMa"].Value.ToString();
                person.Ten = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colTen"].Value.ToString();
                person.DiaChi = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colDiaChi"].Value.ToString();
                person.HoKhau = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHoKhau"].Value.ToString();
                person.NgaySinh = DateTime.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNgaySinh"].Value.ToString());
                person.CMND = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colCMND"].Value.ToString();
                person.DienThoai = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colDienThoai"].Value.ToString();
                person.MaSoThue = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMaSoThue"].Value.ToString();
                person.NgheNghiep = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNgheNghiep"].Value.ToString();
                person.NoiLamViec = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colNoiLamViec"].Value.ToString();
                person.ThuNhap = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colThuNhapBinhQuan"].Value.ToString());
                person.HonNhan = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHonNhan"].Value.ToString();
                person.ThuNhapGiaDinh = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colThuNhapGiaDinh"].Value.ToString());
                person.SoNguoiPhuThuoc = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colSoLuongPhuThuoc"].Value.ToString());
                person.TrinhDo = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colTrinhDo"].Value.ToString();
                person.SoNamLamViec = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colSoNamLamViec"].Value.ToString());
                person.HanMuc = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colHanMuc"].Value.ToString());
                person.MaNhanVien = Int32.Parse(dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMaNhanVien"].Value.ToString());

                frmPersonal.ChiTietCaNhan(person);
                frm.Controls.Add(frmPersonal);
                frmPersonal.Dock = DockStyle.Fill;
                frmPersonal.BringToFront();
            }
            else
            {
                if (dgvDoanhNghiep.CurrentRow == null)
                    return;
                ucCompany frmCompany = new ucCompany(FrmMain.ExeAction.View, this);
                Company company = new Company();
                company.Ma = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaDoanhNghiep"].Value.ToString();
                company.Ten = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenDoanhNghiep"].Value.ToString();
                company.DiaChi = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colDiaChiDN"].Value.ToString();
                company.DienThoai = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colDienThoaiDN"].Value.ToString();
                company.Fax = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colFax"].Value.ToString();
                company.MaSoThue = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaSoThueDN"].Value.ToString();
                company.QuyetDinhThanhLap = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colQuyetDinhThanhLap"].Value.ToString();
                company.NgayQuyetDinh = DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNgayQuyetDinh"].Value.ToString());
                company.CoQuanCap = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colCoQuanCap"].Value.ToString();
                company.CoQuanQLyTrucTiep = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colCoQuanQLyTrucTiep"].Value.ToString();
                company.LoaiHinh = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colLoaiHinh"].Value.ToString();
                company.NganhKinhDoanh = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNganhKinhDoanh"].Value.ToString();
                company.GiayPhepKinhDoanh = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colGiayPhepKinhDoanh"].Value.ToString();
                company.NgayCapGiayPhep = DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNgayCapGiayPhep"].Value.ToString());
                company.NganhKinhTe = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNganhKinhTe"].Value.ToString();
                company.ChuTichHDQT = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenChuTichHDQT"].Value.ToString();
                company.NamSinhChuTichHDQT = DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNamSinhChuTichHDQT"].Value.ToString());
                company.TongGiamDoc = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTenTongGiamDoc"].Value.ToString();
                company.NamSinhTongGiamDoc = DateTime.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colNamSinhTongGiamDoc"].Value.ToString());
                company.TongLaoDong = Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colTongSoLaoDong"].Value.ToString());
                company.VonDieuLe = Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colVonDieuLe"].Value.ToString());
                company.VonKinhDoanh = Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colVonKinhDoanh"].Value.ToString());
                company.HanMuc = Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colHanMucDN"].Value.ToString());
                company.MaNhanVien = Int32.Parse(dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaNhanVienDN"].Value.ToString());

                frmCompany.ChiTietDoanhNghiep(company);
                frm.Controls.Add(frmCompany);
                frmCompany.Dock = DockStyle.Fill;
                frmCompany.BringToFront();
            }
        }

        private void cmbLoaiKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
                dgvCaNhan.BringToFront();
            else
                dgvDoanhNghiep.BringToFront();
            txtTimKiem.Clear();
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
            {
                ucPersonal frmPersonal = new ucPersonal(FrmMain.ExeAction.Add, this);
                frm.Controls.Add(frmPersonal);
                frmPersonal.Dock = DockStyle.Fill;
                frmPersonal.BringToFront();
            }
            else
            {
                ucCompany frmCompany = new ucCompany(FrmMain.ExeAction.View, this);
                frm.Controls.Add(frmCompany);
                frmCompany.Dock = DockStyle.Fill;
                frmCompany.BringToFront();
            }
        }

        private void btnXoaKhachHang_Click(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
            {
                if (dgvCaNhan.CurrentRow == null)
                    return;
                
                try{
                    String makhcanhan = dgvCaNhan.Rows[dgvCaNhan.SelectedRows[0].Index].Cells["colMa"].Value.ToString();
                    if (MessageBox.Show("Bạn có chắc xóa khách hàng này không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        PersonalBUS.XoaKHCaNhan(makhcanhan);
                        LoadCustomer(status);
                        MessageBox.Show("Xóa thành công");
                    }
                }catch(Exception ex){
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                if (dgvDoanhNghiep.CurrentRow == null)
                    return;
                try
                {
                    String makhdoanhnghiep = dgvDoanhNghiep.Rows[dgvDoanhNghiep.SelectedRows[0].Index].Cells["colMaDoanhNghiep"].Value.ToString();
                    if (MessageBox.Show("Bạn có chắc xóa khách hàng này không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        CompanyBUS.XoaKHDoanhNghiep(makhdoanhnghiep);
                        LoadCustomer(status);
                        MessageBox.Show("Xóa thành công");

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        string filterNV = "";
        string filterTCCN = "";
        string filterTCDN = "";
        string and = " and ";
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (cmbLoaiKhachHang.SelectedIndex == 0)
            {
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                if(filterNV == "")
                    dtCaNhan.DefaultView.RowFilter = filterTCCN + txtTimKiem.Text + "%'";
                else
                    dtCaNhan.DefaultView.RowFilter = filterNV + and + filterTCCN + txtTimKiem.Text + "%'";
            }
            else
            {
                if (filterNV == "")
                    dtDoanhNghiep.DefaultView.RowFilter = filterTCDN + txtTimKiem.Text + "%'";
                else
                    dtDoanhNghiep.DefaultView.RowFilter = filterNV + and + filterTCDN + txtTimKiem.Text + "%'";
                dtCaNhan.DefaultView.RowFilter = filterNV;
            }
        }

        private void cmbNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhanVien.SelectedIndex == cmbNhanVien.Items.Count - 1)
                    filterNV = "";
                else
                    filterNV = "MaNhanVien = " + cmbNhanVien.SelectedValue;
                dtCaNhan.DefaultView.RowFilter = filterNV;
                dtDoanhNghiep.DefaultView.RowFilter = filterNV;
                txtTimKiem.Clear();
            }
            catch { }
        }

        private void cmbTieuChiCaNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbTieuChiCaNhan.SelectedIndex == 0)
                {
                    filterTCCN = "Makhcanhan like '%";
                    filterTCDN = "Makhdoanhnghiep like '%";
                }
                else if (cmbTieuChiCaNhan.SelectedIndex == 1)
                {
                    filterTCCN = "Tenkhcanhan like '%";
                    filterTCDN = "Tendoanhnghiep like '%";
                }
                else
                {
                    filterTCCN = filterTCDN = "masothue like '%";
                }
                txtTimKiem.Clear();
               
            }
            catch { }
        }
    }
}
